<?php

/**
 * This class collects the reCAPTCHA settings that the API exposes, provides
 * setters/getters with validation, and provides output methods to get the
 * settings either as a variable (typical use) or JSON notation (the AJAX API).
 */
class reCaptchaSettings {
    private $lang = 'en';
    private $theme = 'red';
    private $custom_translations = null;
    private $custom_theme_widget = null;
    private $tabindex = 0;
    private static $languages = array('en', 'nl','fr', 'de', 'pt', 'ru', 'es', 'tr');
    private static $themes = array('red', 'white', 'blackglass', 'clean', 'custom');
    private static $strings = array('instructions_visual', 'instructions_audio', 'play_again', 'cant_hear_this', 'visual_challenge', 'audio_challenge', 'refresh_btn', 'help_btn', 'incorrect_try_again');
    private static $settings = array('lang', 'theme', 'custom_translations', 'custom_theme_widget', 'tabindex');

    /**
     * Creates an HTML script element that creates the RecaptchaOptions variable. 
     * @return string The SCRIPT tag containing the reCAPTCHA settings variable.
     */
    public function __toString()
    {
        return '<script type="text/javascript">
            var RecaptchaOptions = '.$this->toJSON().
            '</script>';
    }
    /**
     * Set the interface language. Must be one of the supported languages.
     * @param string $lang The selected language.
     */
    public function setLanguage($lang)
    {
        if(!in_array($lang, self::$languages))
        {
            throw new reCaptchaException("Invalid language: '${lang}'");
        }
        $this->lang = $lang;
    }
    
    public function getLanguage()
    {
        return $this->lang;
    }
    
    /**
     * This method allows you to specify the tabindex of the reCAPTCHA field for
     * integration into your form.
     * @param int $i The desired tabindex for the reCAPTCHA input.
     */
    
    public function setTabIndex($i)
    {
        $this->tabindex = $i;
    }
    
    public function getTabIndex()
    {
        return $this->tabindex;
    }
    
    /**
     * Customize one or more of the reCAPTCHA messages. Any messages not specified
     * will use the default for the selected language.
     * @param array $trans a key-value pair for the translation strings. Keys
     * should match the reCaptchaSetttings::$strings, and values are the translated
     * or customized strings.
     */
    public function setCustomTranslations(array $trans)
    {
        if(empty($trans))
            $this->custom_translations = null;
        else
        {
            $this->custom_translations = array();
            foreach($trans as $k => $v)
            {
                $k = strtolower($k);
                if(in_array($k, self::$strings))
                {
                    $this->custom_translations[$k] = $v;
                } else {
                    throw new reCaptchaException("Invalid translation string '${k}'");
                }
            }
        }
    }
    
    public function getCustomTranslations()
    {
        if(empty($this->custom_translations))
          return array();
        return $this->custom_translations;
    }
    
    /**
     * Set the theme.
     * @param string $theme should match one of the entries in reCaptchaSettings::$themes.
     */
    public function setTheme($theme)
    {
        if(!in_array($theme, self::$themes))
        {
            throw new reCaptchaException("Invalid theme: '${theme}'");
        }
        $this->theme = $theme;
    }
    
    public function getTheme()
    {
        return $this->theme;
    }
    
    public function setCustomThemeWidget($id)
    {
        if(empty($id))
            $this->custom_theme_widget = null;
        $this->custom_theme_widget = $id;
    }
    
    public function getCustomThemeWidget()
    {
        return $this->custom_theme_widget;
    }
    
    /**
     * Render the settings to a JavaScript object (JSON).
     * @return string the settings as a JavaScript object (JSON).
     */
    public function toJSON()
    {
        $result = array(
            'lang' => $this->lang,
            'theme' => $this->theme,
            'custom_translations' => $this->custom_translations,
            'custom_theme_widget' => $this->custom_theme_widget,
            'tabindex' => $this->tabindex
        );
        return json_encode($result);
    }
}